var app;
var help = require('./help.js');
/**
 * 自动登录功能
 */
function getUserInfo(obj) {
  app = obj;
  app.data.userInfo = null;
  //调用登录接口
  wx.login({
    success: function (res) {
      var code = res.code;
      autoLogin(code);
    }
  });
}
function autoLogin(code) {
  var scene = help.getCache('scene');
  var scene_gid = help.getCache('scene_gid');
  wx.request({
    url: app.data.apiUrl + 'Weapp/autoLogin',
    method: 'post',
    data: {
      code: code,
      scene: scene,
      scene_gid: scene_gid,
    },
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      wx.hideLoading()
      var data = res.data;
      if (data.code == 1) {
        app.data.userInfo = data.data.userinfo;
        app.data.session_key = data.data.thirdinfo.session_key;
        app.data.openid = data.data.thirdinfo.openid;
        help.setCache('userInfo', data.data.userinfo);
        if (typeof app.getPhoneNumber === 'function') {
          app.getPhoneNumber()
        } else {
          help.goUrlTab('/pages/index/index')
        }
      } else {
        return false;
      }
    },
    fail: function (e) {
      wx.showToast({
        title: '网络异常！err:自动登录失败',
        icon: 'none',
        duration: 1000
      });
    },
  });
}

module.exports.getUserInfo = getUserInfo